package com.lapelittle.framework.model.page;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.Collections;

@Schema(description = "分页结果")
public class PageResult<T> {
    
    @Schema(description = "总记录数")
    private Long total;
    
    @Schema(description = "总页数")
    private Integer pages;
    
    @Schema(description = "当前页码")
    private Integer pageNum;
    
    @Schema(description = "每页数量")
    private Integer pageSize;
    
    @Schema(description = "数据列表")
    private List<T> list;

    public PageResult() {}
    
    public PageResult(PageRequest pageRequest, Long total, List<T> list) {
        this.total = total;
        this.pageNum = pageRequest.getPageNum();
        this.pageSize = pageRequest.getPageSize();
        this.list = list;
        this.pages = (int) ((total + pageSize - 1) / pageSize);
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        this.pages = pages;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    /**
 * 是否有上一页
 */
public boolean hasPrevious() {
    return pageNum > 1;
}

/**
 * 是否有下一页
 */
public boolean hasNext() {
    return pageNum < pages;
}

/**
 * 获取上一页页码
 */
public int getPreviousPage() {
    return hasPrevious() ? pageNum - 1 : pageNum;
}

/**
 * 获取下一页页码
 */
public int getNextPage() {
    return hasNext() ? pageNum + 1 : pageNum;
}

/**
 * 获取导航页码数组
 */
public int[] getNavigatePageNums() {
    int navigatePages = 5; // 导航页码数
    int startPage = Math.max(1, pageNum - navigatePages/2);
    int endPage = Math.min(pages, startPage + navigatePages - 1);
    
    startPage = Math.max(1, endPage - navigatePages + 1);
    
    int[] nums = new int[endPage - startPage + 1];
    for (int i = 0; i < nums.length; i++) {
        nums[i] = startPage + i;
    }
    return nums;
}

    public static <T> PageResult<T> empty() {
        return new PageResult<>(new PageRequest(), 0L, Collections.emptyList());
    }
}